<?php
/*
* Copyright (c) 2010 by Cediti
*
* You may distribute under the terms of either the GNU Lesser General
* Public License or the new BSD License, as specified in the README file.
*/
// created :	9 February 2010
// version :	9 February 2010

class Cmis_ServiceUtils
{
	/**
	 * Replace {Field} by a value from the array, field that are not in the array are put empty
	 * @param string $template
	 * @param array $values [field1:value, field2:value,  ...]
	 * @return string
	 */
	public static function urlTemplateParse($template, $values = array())
	{
		$template = str_replace('&amp;', '&', $template);
		if (!empty($values) && is_array($values))
		{
			foreach ($values as $field => $value)
			{
				if (is_bool($value))
				{
					$value = ($value)? 'true':'false';
				}
				$template = str_replace('{' . $field . '}', urlencode($value), $template);
			}
		}

		//we put empty value for non-set field
		return preg_replace('/{[a-zA-Z0-9_]+}/', '', $template);
	}

	/**
	 * Build an url
	 *
	 * If parameters contains array they will be converted
	 * All value are urlencoded
	 *
	 * @param string $baseUrl
	 * @param array $params [get1:value1, get2:value2]
	 * @return string
	 */
	public static function buildUrl($baseUrl, $params)
	{
		foreach ($params as $field => &$value)
		{
			if (is_bool($value))
			{
				$value = ($value)? 'true':'false';
			}
		}
		return $baseUrl . (strpos($baseUrl, '?')?'&':'?') . http_build_query($params);
	}

	/**
	 * Make a recursive array from a xml
	 * @return array
	 */
	public static function xml2Array(SimpleXMLIterator $xml)
	{
		$ret = array();
		foreach($xml as $key => $info)
		{
			if ($xml->hasChildren())//iteror need the reference to the current object => so ...
			{
				if (isset($ret[ $key ]))
				{
					if (!isset($ret[ $key ][0]))
					{
						$tmp = $ret[ $key ];
						$ret[ $key ] = array();
						$ret[ $key ][] = $tmp;
					}
					$ret[ $key ][] = self::xml2Array($info);
				}
				else
				{
					$ret[ $key ] = self::xml2Array($info);
				}
			}
			else
			{
				$ret[ $key ] = (string)$info;
			}
		}
		return $ret;
	}

	/**
	 * Walk into array
	 *
	 * If a rel is set, an array is create to put more than one
	 *
	 * @param SimpleXMLElement $links
	 * @param array $_link ref array that were modified
	 * @return void
	 */
	public static function linksWalk(SimpleXMLElement $links, &$_link)
	{
		foreach($links as $link)
		{
			$lnk = array();
			$attributes = $link->attributes();

			foreach ($attributes as $atrib => $v)
			{
				$lnk[ $atrib ] = (string)$v;
			}

			if (isset($_link[ $lnk['rel'] ]))
			{
				//not already an array?
				if (isset($_link[ $lnk['rel'] ]['rel']))
				{
					$t = $_link[ $lnk['rel'] ];
					unset($_link[ $lnk['rel'] ]);
					$_link[ $lnk['rel'] ]['multiple'] = true;
					$_link[ $lnk['rel'] ][] = $t;
				}
				$_link[ $lnk['rel'] ][] = $lnk;
			}
			else
			{
				$_link[ $lnk['rel'] ] = $lnk;
				$_link[ $lnk['rel'] ]['multiple'] = false;
			}
		}
	}

	/**
	 * Replace some special char like mysql_escape_string (that was deprecated)
	 * @param string $sql
	 * @param bool $like
	 * @return string
	 */
	public static function sqlEscape($sql, $like = true)
	{
		$search = array(
            '\\',
			"'",
			"\x00",
			"\n",
			"\r",
			"\x1a"
		);

		$replace = array(
            '\\\\',
			"\'",
	        '\\x00',
	        '\\n',
	        '\\r',
	        '\\x1a'
		);

		if ($like)
		{
            $search[] = '%';
			$replace[] = '\%';
		}
		else
		{
			$search[] = '%';
			$replace[] = '';
		}

		return str_replace($search, $replace, $sql);
	}
}

# EOF
